#
# example014.py
#
# Adaptive Speech watermarking in wavelet domain by using LSB
#
# Copyright (C) 2012 Robert Buj Gelonch
# Copyright (C) 2012 David Megias Jimenez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
__author__ = "Robert Buj Gelonch, and David Megias Jimenez"
__copyright__ = "Copyright 2012, Robert Buj Gelonch and David Megias Jimenez"
__credits__ = ["Robert Buj Gelonch", "David Megias Jimenez"]
__license__ = "GPL"
__version__ = "3"
__maintainer__ = "Robert Buj"
__email__ = "rbuj@uoc.edu"
__status__ = "Development"
__docformat__ = 'plaintext'

import sys
import time

import getopt
import numpy
from scipy.io.wavfile import read
from scipy.io.wavfile import write
from uoc.wave.watermark.message import bitarrray
from uoc.wave.watermark.schemes import wt_lsb as WS
from uoc.wave.watermark.schemes.generic import detection_function
from uoc.wave.watermark.schemes.generic import embedding_function
from uoc.wave.watermark.schemes.generic import retrieval_function

#--------------------------------------------------------------
# Parameters of the watermarking
#--------------------------------------------------------------
M = "hello!" # secret message in the watermark, string

def example014(inputfile):
    print "example014.py"
    print
    print "WS: DWT, then LSB Watermarking on cA"
    print
    #--------------------------------------------------------------
    # Read an audio file
    #--------------------------------------------------------------
    (sample_rate, S) = read(inputfile)
    #--------------------------------------------------------------
    # Add the watermark
    #--------------------------------------------------------------
    start = time.clock()
    s = embedding_function(S, \
                           bitarrray(M), \
                           watermarking_sheme=WS.embedding_function, \
                           frame_size=4, \
                           numtaps='db10')
    print 'Time elapsed E = ', time.clock() - start, 's'
    write("../example014.E.wav", sample_rate / 2, s.astype(numpy.int16))
    #--------------------------------------------------------------
    # Identify the message
    #--------------------------------------------------------------
    start = time.clock()
    m = retrieval_function(s, \
                           watermarking_sheme=WS.retrieval_function, \
                           frame_size=4)
    print 'Time elapsed R = ', time.clock() - start, 's'
    #--------------------------------------------------------------
    # Detection
    #--------------------------------------------------------------
    start = time.clock()
    d = detection_function(s, \
                           bitarrray(M), \
                           watermarking_sheme=WS.detection_function, \
                           frame_size=4, \
                           message=m)
    print 'Time elapsed D = ', time.clock() - start, 's [', d, ']'

def main(argv):
    inputfile = '../../../media/OSR_uk_000_0020_8k.wav'
    try:
        opts, args = getopt.getopt(argv, "hi:o:", ["ifile="])
    except getopt.GetoptError:
        print 'example014.py -i <inputfile>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'example014.py -i <inputfile>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
    example014(inputfile)

if __name__ == "__main__":
    main(sys.argv[1:])
    print "Done"